package com.framework.app;

import android.arch.lifecycle.ViewModelProvider;
import android.databinding.DataBindingUtil;
import android.graphics.RectF;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.framework.app.databinding.FragmentMainBinding;
import com.framework.diarylibrary.DataInterpreter;
import com.framework.diarylibrary.DiaryItemEntity;
import com.framework.diarylibrary.WeekView;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

public class MainFragment extends Fragment implements WeekView.DiaryClickListener, WeekView.DiaryLongPressListener, WeekView.EmptyViewLongPressListener {

    private MainViewModel mViewModel;
    FragmentMainBinding binding;
    int dayyy =1;
    private static final int TYPE_DAY_VIEW = 1;
    private static final int TYPE_THREE_DAY_VIEW = 2;
    private static final int TYPE_WEEK_VIEW = 3;
    private int mWeekViewType = TYPE_THREE_DAY_VIEW;
    private MainViewModel viewModel;
//    List<DiaryItemEntity> events = new ArrayList<>();
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_main, container, false);
        viewModel = new ViewModelProvider(getActivity(), new ViewModelProvider.AndroidViewModelFactory(getActivity().getApplication())).get(MainViewModel.class);
        binding.setViewmodel(viewModel);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        binding.weekView.setOnDiaryClickListener(this);
        binding.weekView.setDiaryLongPressListener(this);
        binding.weekView.setEmptyViewLongPressListener(this);
        setupDateTimeInterpreter(true);

        mWeekViewType = TYPE_DAY_VIEW;
        binding.weekView.setColumnGap((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources().getDisplayMetrics()));
        binding.weekView.setTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 12, getResources().getDisplayMetrics()));
        binding.weekView.setDiaryTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 12, getResources().getDisplayMetrics()));
        binding.weekView.setHorizontalFlingEnabled(true);
        viewModel.addTestData();

        binding.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Calendar startTime = Calendar.getInstance();
                startTime.set(Calendar.DAY_OF_MONTH, dayyy++);
                startTime.set(Calendar.MONTH, 7 - 1);
                startTime.set(Calendar.YEAR, 2020);
                binding.weekView.goToDate(startTime);
            }
        });

    }

    private void setupDateTimeInterpreter(final boolean shortDate) {
        binding.weekView.setDateTimeInterpreter(new DataInterpreter() {
            @Override
            public String interpretDate(Calendar date) {
                SimpleDateFormat weekdayNameFormat = new SimpleDateFormat("EEEEEE", Locale.getDefault());
                String weekday = weekdayNameFormat.format(date.getTime());
                SimpleDateFormat format = new SimpleDateFormat("MM月dd日", Locale.getDefault());

                String formatTitle = "%s %s";
                return String.format(formatTitle, format.format(date.getTime()), weekday.toUpperCase());
            }

            @Override
            public String interpretTime(int hour) {
                if (hour < 10) {
                    return "0" + hour + ":00";
                }
                return hour + ":00";
            }
        });
    }

    protected String getDiaryTitle(Calendar time) {
        return String.format("标题是 %s月%d日 %02d:%02d ", time.get(Calendar.MONTH) + 1, time.get(Calendar.DAY_OF_MONTH), time.get(Calendar.HOUR_OF_DAY), time.get(Calendar.MINUTE));
    }

    @Override
    public void onDiaryClick(DiaryItemEntity event, RectF eventRect) {
        Toast.makeText(getActivity(), "Clicked " + event.getName(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDiaryLongPress(DiaryItemEntity event, RectF eventRect) {
        Toast.makeText(getActivity(), "Long pressed" + event.getName(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onEmptyViewLongPress(Calendar time) {
        Toast.makeText(getActivity(), "Empty Long pressed: " + getDiaryTitle(time), Toast.LENGTH_SHORT).show();
    }
}